package com.zb.service.impl;
import java.math.BigDecimal;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.TbSeckillGoodsModel;
import com.zb.entity.TbSeckillOrderModel;
import com.zb.mapper.TbSeckillOrderMapper;
import com.zb.service.TbSeckillGoodsService;
import com.zb.service.TbSeckillOrderService;
import com.zb.task.MultiThreadingCreateOrder;
import com.zb.vo.SeckillStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @ClassName TbSeckillOrderServiceImpl
 * @Description 服务接口实现
 * @Author zj
 * @Date 2022/11/16 09:31
 **/
@Service
public class TbSeckillOrderServiceImpl extends ServiceImpl<TbSeckillOrderMapper, TbSeckillOrderModel> implements TbSeckillOrderService {

    @Autowired
    private MultiThreadingCreateOrder multiThreadingCreateOrder;


    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public Integer addOrder(String id, String time, String username) {
        Long userCount = redisTemplate.boundHashOps("userCount").increment(username, 1);
        if (userCount>1){
            return 2;
        }
        System.out.println("业务订单方法开始");
        SeckillStatus seckillStatus = new SeckillStatus(username,1,id,time);
        //用户购买商品排队的队列
        redisTemplate.boundListOps("seckillQueue").leftPushAll(seckillStatus);
        //每个用户的状态对象
        redisTemplate.boundHashOps("seckillStatus").put(username,seckillStatus);
        multiThreadingCreateOrder.createOrder();
        System.out.println("业务订单方法结束");
        return 1;
    }


    @Override
    public SeckillStatus orderStatus(String username) {
        SeckillStatus seckillStatus = (SeckillStatus) redisTemplate.boundHashOps("seckillStatus").get(username);
        return seckillStatus;
    }
}
